﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RipPoll.Library;

namespace RipPoll.Web
{
    public partial class ViewPolls : RippollBasePage
    {
        List<PollPE> polls = new List<PollPE>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);            
            polls = GetUnApprovedPolls();
        }

        protected void Page_Load(object sender, EventArgs e)
        {   
            if (this.Master != null)
                ((RippollMaster)this.Master).PageTitle = "View Polls";

            GridView1.ShowHeader = true;

            if (!IsPostBack)
            {   
                BindGrid(polls);
            }

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0 && polls != null && e.Row.RowIndex < polls.Count)
            {
                PollPE ppe = polls[e.Row.RowIndex];
                e.Row.Cells[0].Text = ppe.Name;
                e.Row.Cells[1].Text = ppe.Category.Category;
                Panel p = new Panel();
                p.ID = "choicepanefor" + e.Row.RowIndex.ToString();
                for (int i = 0; i < ppe.Choices.Count; i++)
                {
                    ChoicePE cpe = ppe.Choices[i];
                    Label l = new Label();
                    l.ID = p.ID + "lblChoice" + cpe.Id;
                    l.Text = cpe.Choice;
                    Label l1 = new Label();
                    l1.Text = "<BR/>";
                    e.Row.Cells[2].Controls.Add(l);
                    e.Row.Cells[2].Controls.Add(l1);
                }

                Button btnApprove = (Button)e.Row.Cells[3].FindControl("btnApprove");
                if (btnApprove != null)
                    btnApprove.Attributes.Add("pollId", ppe.Id.ToString());
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            PollServices.PollServicesClient pbo = new PollServices.PollServicesClient();
            pbo.ApprovePoll(int.Parse(b.Attributes["pollId"].ToString()));
            BindGrid(GetUnApprovedPolls());
        }

        List<PollPE> GetUnApprovedPolls()
        {
            PollServices.PollServicesClient pbo = new PollServices.PollServicesClient();
            return pbo.GetUnApprovedPolls();
        }

        private void BindGrid(List<PollPE> plist)    
        {
            if(plist != null && plist.Count > 0)
            {
                GridView1.DataSource = plist;
                GridView1.DataBind();
            }
            else
            {
                PollPE p = new PollPE();
                p.Name = "No un approved polls exists";
                List<PollPE> newpolls = new List<PollPE>();
                newpolls.Add(p);

                GridView1.DataSource = newpolls;
                GridView1.DataBind();
                int columnCount = GridView1.HeaderRow.Cells.Count;
                //Call the clear method to clear out any controls that you use in the columns.  
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
                GridView1.Rows[0].Cells[0].Text = "No un approved polls exists";
                return;
            }
        }
        

    }
}